--[[
  兴业银行 - 互金平台 - DUBBO
]]
require("apm_core")
require("ApmJson")
require("extension")
require("functions")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local s_channel
local s_channel_name

local dict_transtype = {}
local dict_channel = {}

local function parseIp(ip)
    local _, _, value = string.find(ip, "(%d+.%d+)$")
    return value == nil and "" or value
end

function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 20880 then return end
    if ApmData.getMsgType() ~= 1 then return end

    --    logInfo("DUBBO:["..KGREEN.."%s"..RESET.."]", ApmData.dumpFields(true))

    local s_transtype
    local s_transtype_name
    local s_rspcode
    local s_rspmsg

    -- 交易类型
    local s_path = ApmData.getField("request.rpc.path", 1)
    s_path = shortcutjavaclassname(s_path)
    local s_function = ApmData.getField("request.rpc.function", 1)
    if s_path ~= nil and s_function ~= nil then
        s_transtype = s_path .. "::" .. s_function
        if s_transtype ~= nil then
            s_transtype = string.trim(s_transtype)
            s_transtype_name = dict_transtype[s_transtype]
            if s_transtype_name == nil then s_transtype_name = s_transtype end
        end
    end

    local s_catRootMessageId
    local s_catParentMessageId
    local s_catChildMessageId

    local i_req_arg_size = ApmData.getField("request.rpc.arg.size", 1)
    if i_req_arg_size ~= nil and i_req_arg_size ~= "" and type(i_req_arg_size) == 'string' then
        i_req_arg_size = tonumber(i_req_arg_size)
        if i_req_arg_size > 0 then
            for i=1, i_req_arg_size do
                if s_catRootMessageId == nil then
                    s_catRootMessageId = ApmData.getField("request.rpc.arg." .. i .. "._catRootMessageId", 1)
                end
                if s_catParentMessageId == nil then
                    s_catParentMessageId = ApmData.getField("request.rpc.arg." .. i .. "._catParentMessageId", 1)
                end
                if s_catChildMessageId == nil then
                    s_catChildMessageId = ApmData.getField("request.rpc.arg." .. i .. "._catChildMessageId", 1)
                end
            end
        end
    end

    local s_response_code = ApmData.getField("response.code", 1)
    local s_response_type = ApmData.getField("response.rpc.arg.type", 1)
    if s_rspcode == nil then
        s_rspcode = ApmData.getField("response.rpc.arg.fields.responseCode", 1)
        s_rspmsg = ApmData.getField("response.rpc.arg.fields.errorInfo", 1)
    end
    if s_rspcode == nil then
        s_rspcode = ApmData.getField("response.rpc.arg.fields.xym", 1)
        s_rspmsg = ApmData.getField("response.rpc.arg.fields.xyxx", 1)
    end
    if s_rspcode == nil then
        s_rspcode = ApmData.getField("response.rpc.arg.fields.msgid", 1)
        s_rspmsg = ApmData.getField("response.rpc.arg.fields.sbxx", 1)
    end
    if s_rspcode == nil then
        s_rspcode = ApmData.getField("response.rpc.arg.fields.errorCode", 1)
        s_rspmsg = ApmData.getField("response.rpc.arg.fields.description", 1)
    end
    if s_rspcode == nil then
        s_rspcode = ApmData.getField("response.rpc.arg.fields.code", 1)
        s_rspmsg = ApmData.getField("response.rpc.arg.fields.msg", 1)
    end
    if s_response_type ~= nil and s_rspcode == nil
            and s_response_type ~= "com.cib.channel.service.api.vo.RedisParamVO"
            and s_response_type ~= "com.ibm.member.service.entity.MemberBaseVO"
            and s_response_type ~= "com.ibm.mdseview.service.vo.MdseMainInfo"
            and s_response_type ~= "com.ibm.multiorg.service.common.model.vo.LKBankInfoVO"
--            and s_response_type ~= "com.ibm.capabilitycenter.service.vo.account.AccountBalanceResultVO"
            --            and s_response_type ~= "com.ibm.capabilitycenter.service.vo.account.AccountListResultVO"
--            and s_response_type ~= "com.ibm.capabilitycenter.service.vo.ebank.account.WalletBalanceResultVO"
        --            and s_response_type ~= "com.ibm.capabilitycenter.service.vo.ebank.contractorder.QuerySignContractResultVO"
    then
        logError("DUBBO:["..KGREEN.."%s"..RESET.."]", ApmData.dumpFields(true))
    end

    --    logDebug("_catRootMessageId:[%s]", s_catRootMessageId)
    --    logDebug("_catParentMessageId:[%s]", s_catParentMessageId)
    --    logDebug("_catChildMessageId:[%s]", s_catChildMessageId)
    --    if s_transtype == nil or s_catRootMessageId == nil then
    if s_transtype == nil then
        logError("DUBBO:["..KGREEN.."%s"..RESET.."]", ApmData.dumpFields(true))
    else
        ApmData.replaceField("transaction.type_code", s_transtype)
        ApmData.replaceField("transaction.type_name", s_transtype_name)

        if s_channel ~= nil then
            ApmData.replaceField("transaction.channel_code", s_channel)
            ApmData.replaceField("transaction.channel_name", s_channel_name)
        end

        if s_catRootMessageId ~= nil then
            ApmData.replaceField("transaction.code", s_catRootMessageId)
        end

        if s_rspcode ~= nil and s_rspcode ~= "" then
            ApmData.replaceField("response.code_string", s_rspcode)
            ApmData.delFields("response.code")
        end

        if s_rspmsg ~= nil and s_rspmsg ~= "" then
            ApmData.replaceField("response.error_message", s_rspmsg)
        end

        --        logInfo("dubbo(%s:%d->%s:%d) name:%s|channel:%s|seq:%s|ret:%s|%s",
        --            ApmData.getSrcIp(),
        --            ApmData.getSrcPort(),
        --            parseIp(ApmData.getDstIp()),
        --            ApmData.getDstPort(),
        --            s_transtype_name,
        --            s_channel_name,
        --            s_catRootMessageId,
        --            s_rspcode,
        --            s_rspmsg
        --        )
    end

    return
end
--互金平台 - DUBBO
--v1.0.5
